import urlConfig from '../../config/url.json';
import Axios from '../../config/axios';
import store from '../../store/index';
import {COMMONCODEPROVINCE} from "../../store/mutation-types";

let commonCodeList={};
commonCodeList.getcomDictionaryLst=function() {
    let nowTime = new Date ();
    let updateTime = localStorage.getItem('updateTime');
    updateTime=new Date(updateTime);
    let Millisecond= nowTime.getTime() - updateTime.getTime();//相差毫秒数
    let minutes=Millisecond/(60*1000);
    //这里判断相差几分钟，判断相差几小时除（60*60*1000）60个1分钟的意思，天就是24个1小时，然后if里也要对应修改
    if(minutes>30||!localStorage.getItem('comDictionaryLst')) {
        Axios({
                method: 'post',
                url: urlConfig.manageCache,
                baseURL: '/api/background',
                data: {
                    // updateTime:updateTime,
                    updateTime: '',
                    groupList: [{
                        groupCode: 'administrative_division',
                        parentCode: ''
                    }]
                },
            }
        ).then(function (response) {

            if (response.data.code > 199 && response.data.code < 400) {
                localStorage.setItem('comDictionaryLst', JSON.stringify(response.data.data.comDictionaryLst));
                localStorage.setItem('updateTime', response.data.data.updateTime);
            }
        }).catch(function (err) {
        })
    }
}

commonCodeList.getProvince=function () {
    Axios({
            method: 'post',
            url: urlConfig.manageCache,
            baseURL: '/api/background',
            data: {
                // updateTime:updateTime,
                updateTime: '',
                groupList: [{
                    groupCode: 'administrative_division',
                    parentCode: ''
                }]
            },
        }
    ).then(function (response) {
        if (response.data.code > 199 && response.data.code < 400) {

            let commonCodeProvince=[];
            for(let i=0;i<response.data.data.comDictionaryLst.length;i++){
                if(response.data.data.comDictionaryLst[i].parentCode===''){
                    commonCodeProvince.push(response.data.data.comDictionaryLst[i])
                }
            }
            store.commit(COMMONCODEPROVINCE,commonCodeProvince);
        }
    }).catch(function (err) {
    })
}

commonCodeList.getcommonCodeChildren=function (parentCode) {
    let comDictionaryLst = localStorage.getItem('comDictionaryLst');
    comDictionaryLst=JSON.parse(comDictionaryLst)
    let commonCodeChildren=[];
    for(let i=0;i<comDictionaryLst.length;i++){
        if(comDictionaryLst[i].parentCode===parentCode){
            commonCodeChildren.push(comDictionaryLst[i])
        }
    }
    return commonCodeChildren
}

commonCodeList.getcommonCodeName=function (commonCode) {
    let comDictionaryLst = localStorage.getItem('comDictionaryLst');
    comDictionaryLst=JSON.parse(comDictionaryLst)
    for(let i=0;i<comDictionaryLst.length;i++){
        if(comDictionaryLst[i].itemCode===commonCode){
            return comDictionaryLst[i].itemName;
        }
    }
}



export default commonCodeList;